<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="提现单号" prop="requestId">
        <el-input v-model="queryParams.requestId" placeholder="请输入提现单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="所属银行" prop="tableName">
        <el-select v-model="queryParams.tableName" placeholder="请选择所属银行" clearable>
          <el-option label="蓝海银行" value="ecs_pay_ease" />
          <el-option label="蓝海/苏商/厦门" value="ecs_yee_pay" />
          <el-option label="苏商/华通" value="ecs_baofu_merchant" />
          <el-option label="银联代扣" value="ecs_yee_pay_withholding" />
          <el-option label="绑卡代扣" value="ecs_yeepay_withhold" />
        </el-select>
      </el-form-item>

      <el-form-item label="收款人姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入收款人姓名	" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="银行卡号" prop="bankCardNum">
        <el-input v-model="queryParams.bankCardNum" placeholder="请输入银行卡号	" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="公司名称" prop="shopName">
        <el-input v-model="queryParams.shopName" placeholder="请输入公司名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态 " prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态 " clearable>
          <el-option label="提现中" :value="1" />
          <el-option label="提现成功" :value="2" />
          <el-option label="提现失败" :value="3" />
          <el-option label="提现取消" :value="4" />
          <el-option label="退票" :value="5" />
        </el-select>
      </el-form-item>
      <el-form-item label="提现时间" prop="createTime">
        <el-date-picker clearable v-model="dateRange" type="daterange" range-separator="至" value-format="yyyy-MM-dd"
          :picker-options="pickerOptions" start-placeholder="开始日期" end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['ecs:withdrawalRecord:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="withdrawalRecordList" @selection-change="handleSelectionChange">
      <el-table-column label="提现单号" align="center" prop="requestId" width="280" />
      <el-table-column label="所属银行" align="center" prop="tableName">
        <template slot-scope="scope">
          <span v-if="scope.row.tableName === 'ecs_pay_ease'">蓝海银行</span>
          <span v-else-if="scope.row.tableName === 'ecs_yee_pay'">蓝海/苏商/厦门</span>
          <span v-else-if="scope.row.tableName === 'ecs_din_pay'">智付锡商</span>
          <span v-else-if="scope.row.tableName === 'ecs_baofu_merchant'">苏商/华通</span>
          <span v-else-if="scope.row.tableName === 'ecs_yee_pay_withholding'">银联代扣</span>
          <span v-else-if="scope.row.tableName === 'ecs_yeepay_withhold'">绑卡代扣</span>
        </template>
      </el-table-column>
      <el-table-column label="公司名称" align="center" prop="shopName" width="180" />
      <el-table-column label="收款人姓名" align="center" prop="name" width="150" />
      <el-table-column label="提现角色" v-if="user.userId === 1" align="center" prop="transferCardType" width="90">
        <template slot-scope="{ row }">
          <span v-if="row.roleType === 1">平台</span>
          <span v-if="row.roleType === 2">代理</span>
          <span v-if="row.roleType === 3">商户</span>
          <span v-if="row.roleType === 4">二级代理</span>
        </template>
      </el-table-column>
      <el-table-column label="提现卡类型" align="center" prop="transferCardType" width="90">
        <template slot-scope="{ row }">
          <span v-if="row.transferCardType === 1">个人</span>
          <span v-if="row.transferCardType === 2">对公</span>
        </template>
      </el-table-column>
      <el-table-column label="银行卡号" align="center" prop="bankCardNum" width="180" />
      <el-table-column label="银行名称" align="center" prop="bankName" width="110" />
      <el-table-column label="提现金额" align="center" prop="amount" width="120" />
      <el-table-column label="状态" align="center" prop="status" width="90">
        <template slot-scope="{ row }">
          <span v-if="row.status === 1"><el-tag type="info">提现中</el-tag></span>
          <span v-if="row.status === 2"><el-tag type="success">提现成功</el-tag></span>
          <span v-if="row.status === 3">
            <el-popover :content="row.fail" trigger="click">
              <template #reference>
                <el-tag type="danger">提现失败</el-tag>
              </template>
            </el-popover>
          </span>
          <span v-if="row.status === 4"><el-tag type="warning">提现取消</el-tag></span>
          <span v-if="row.status === 5"><el-tag type="warning">退票</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column label="提现时间" align="center" prop="createTime" width="180" />
      <el-table-column label="到账时间" align="center" prop="paymentDate" width="180"></el-table-column>
      <el-table-column label="提现备注" align="center" prop="remark" />
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import { listWithdrawalRecord, getWithdrawalRecord, delWithdrawalRecord, addWithdrawalRecord, updateWithdrawalRecord } from "@/api/ecs/withdrawalRecord";
import { getUserProfile } from '@/api/system/user'
import { getAgentUserId } from '@/api/ecs/agent'

export default {
  name: "WithdrawalRecord",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 提现记录表格数据
      withdrawalRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      dateRange: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        balaceId: null,
        requestId: null,
        userId: null,
        serialNumber: null,
        name: null,
        transferCardType: null,
        bankCardNum: null,
        bankName: null,
        amount: null,
        shopName: null,
        subMerchantId: null,
        tableName: null,
        status: null,
        createTime: null,
        fail: null,
      },
      //时间快捷范围
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      // 表单参数
      form: {},
      user: {},
      // 表单校验
      rules: {
        requestId: [
          { required: true, message: "提现单号不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "账号编码不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    getUserProfile().then(response => {
      this.user = response.data;
      if (this.user.deptId === 101) {
        getAgentUserId(this.user.userId).then(response => {
          if (response.data.agentId !== 0) {
            this.user.deptId = 102
          }
        });
      }
    });
    this.getList();
  },
  methods: {
    /** 查询提现记录列表 */
    getList() {
      this.loading = true;
      listWithdrawalRecord(this.addDateRange(this.queryParams, this.dateRange, "CreateTime")).then(response => {
        this.withdrawalRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加提现记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWithdrawalRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改提现记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWithdrawalRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWithdrawalRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除提现记录编号为"' + ids + '"的数据项？').then(function () {
        return delWithdrawalRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ecs/withdrawalRecord/export', {
        ...this.queryParams
      }, `提现记录_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
